
//#include "StateShop.h"
#include "StateShopAnimals.h"
#include "StateCoinsPopUp.h"
#include "StateShopAnimalDetail.h"
#include "StatePopUp.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"
#include "LevelManager.h"

void StateShopAnimals::loadContent()
{
    StateShopCommon::loadContent();
    
    m_oTitleText->setText("Babies");

    for(int i=0; i<MAX_BABY_TYPES; ++i)
    {
        BabyData sBabyData = LevelManager::instance().getBabyData(i);
        
        Vector2 vPos = Vector2(80 + 160 * (i % 4), 730 - 140 * (int)(i / 4));
        
        m_oBabyShadow[i] = new RenderableEntity("shadow2", vPos + Vector2(0, -60));
        m_oBabyShadow[i]->setScale(Vector2(0.3f, 0.3f));
        m_oBabyShadow[i]->setCentered();
        m_oBabyShadow[i]->setLiving(true);

        m_oBabyIcon[i] = new AnimatedEntity(sBabyData.sName, vPos);
        m_oBabyIcon[i]->setCentered();
        m_oBabyIcon[i]->setScale(Vector2(0.70f, 0.70f));
        
        m_oBabyText[i] = new TextEntity(sBabyData.sCaption, FONT_NAME, 32, vPos + Vector2(10, -50), 1, 1);
        m_oBabyText[i]->setCentered();
        m_oBabyText[i]->setColor(COLOR_WHITE);
        m_oBabyText[i]->setShadow();
        
        if(sBabyData.iPrice <= 0)
        {
            m_oBabyPrice[i] = new TextEntity("IAP", FONT_NAME, 24, vPos + Vector2(22, -40), 2, 1);
            m_oBabyPrice[i]->setCenter(Vector2(1.0f, 0.5f));
            m_oBabyPrice[i]->setShadow();
            
            m_oBabyCoin[i] = new RenderableEntity("mariquitiquis", vPos + Vector2(40, -40));
            m_oBabyCoin[i]->setScale(Vector2(1.0f, 1.0f));
            m_oBabyCoin[i]->setCentered();
        }
        else
        {
            char buff[10];
            if(sBabyData.iPrice >= 10000)
                sprintf(buff, "%ik", sBabyData.iPrice / 1000);
            else
                sprintf(buff, "%i", sBabyData.iPrice);
            
            m_oBabyPrice[i] = new TextEntity(buff, FONT_NAME, 24, vPos + Vector2(22, -40), 2, 1);
            m_oBabyPrice[i]->setCenter(Vector2(1.0f, 0.5f));
            m_oBabyPrice[i]->setShadow();
            
            m_oBabyCoin[i] = new RenderableEntity("icon_babycoin", vPos + Vector2(40, -40));
            m_oBabyCoin[i]->setScale(Vector2(0.3f, 0.3f));
            m_oBabyCoin[i]->setCentered();
        }
        
        m_oEnter->addInEffect(m_oBabyShadow[i]);
        m_oEnter->addInEffect(m_oBabyIcon[i]);
        m_oEnter->addInEffect(m_oBabyText[i]);
        m_oEnter->addInEffect(m_oBabyPrice[i]);
        m_oEnter->addInEffect(m_oBabyCoin[i]);
        
        m_oExit->addOutEffect(m_oBabyShadow[i]);
        m_oExit->addOutEffect(m_oBabyIcon[i]);
        m_oExit->addOutEffect(m_oBabyText[i]);
        m_oExit->addOutEffect(m_oBabyPrice[i]);
        m_oExit->addOutEffect(m_oBabyCoin[i]);
    }
    
    m_oBabySelectedIcon = new RenderableEntity("selectedpacifier", Vector2(350, 80));
    m_oBabySelectedIcon->setCentered();
    m_oBabySelectedIcon->setScale(Vector2(1.0f, 1.0f));
    m_oBabySelectedIcon->setLiving(true);
    
    
    m_oDetailsIcon = new RenderableEntity("buttonBG", Vector2(300, 80));
    m_oDetailsIcon->setCentered();
    m_oDetailsIcon->setScale(Vector2(0.7f, 0.7f));
    m_oDetailsIcon->changeColorWhenClicked(true);
    
    m_oDetailsText = new TextEntity("Details", FONT_NAME, 40, Vector2(300, 80), 1, 1);
    m_oDetailsText->setCentered();
    m_oDetailsText->setShadow();
    m_oDetailsText->setColor(COLOR_PINK_TEXT);
    
    m_oBuyIcon = new RenderableEntity("buttonBG", Vector2(300, 80));
    m_oBuyIcon->setCentered();
    m_oBuyIcon->setScale(Vector2(0.7f, 0.7f));
    m_oBuyIcon->changeColorWhenClicked(true);
    
    m_oBuyText = new TextEntity("Buy it!", FONT_NAME, 40, Vector2(300, 80), 1, 1);
    m_oBuyText->setCentered();
    m_oBuyText->setShadow();
    m_oBuyText->setColor(COLOR_PINK_TEXT);
    
    m_iSelectedBaby = -1;
    
    m_oEnter->addInEffect(m_oBabySelectedIcon);
    m_oEnter->addInEffect(m_oDetailsIcon);
    m_oEnter->addInEffect(m_oDetailsText);
    m_oEnter->addInEffect(m_oBuyIcon);
    m_oEnter->addInEffect(m_oBuyText);
    
    m_oExit->addOutEffect(m_oBabySelectedIcon);
    m_oExit->addOutEffect(m_oDetailsIcon);
    m_oExit->addOutEffect(m_oDetailsText);
    m_oExit->addOutEffect(m_oBuyIcon);
    m_oExit->addOutEffect(m_oBuyText);
}

void StateShopAnimals::unloadContent()
{
    StateShopCommon::unloadContent();
}

void StateShopAnimals::activated()
{
    StateShopCommon::activated();
}

void StateShopAnimals::enter()
{
    switch(m_ePopupState)
    {
        case SHOPPOPUP_NONE:
            m_iSelectedBaby = -1;
            break;
            
        case SHOPPOPUP_BUY:
            if(StatePopUp::instance().getResult())
            {
                //Bought the baby!
                SaveGame sSaveGame = ProgressManager::instance().getSavedData();
                int iPrice = LevelManager::instance().getBabyData(m_iSelectedBaby).iPrice;
                sSaveGame.iCoins -= iPrice;
                sSaveGame.iCoinsSpent += iPrice;
                sSaveGame.sBabyData[m_iSelectedBaby].bUnlocked = true;
                ProgressManager::instance().setSavedData(sSaveGame);
            }
            break;
        
        default:
            break;
    }

    StateShopCommon::enter();
    
    
    for(int i=0; i<MAX_BABY_TYPES; ++i)
    {
        bool bUnlocked = ProgressManager::instance().getSavedData().sBabyData[i].bUnlocked;
        
        m_oBabyIcon[i]->setColor(bUnlocked ? COLOR_WHITE : COLOR_BLACK);
        m_oBabyText[i]->setRender(bUnlocked);
        
        m_oBabyPrice[i]->setRender(!bUnlocked);
        m_oBabyCoin[i]->setRender(!bUnlocked);
    }
    
    updateButtons();
}

void StateShopAnimals::updateButtons()
{
    if(m_iSelectedBaby == -1)
    {
        m_oDetailsIcon->setRender(false);
        m_oDetailsText->setRender(false);
        m_oBuyIcon->setRender(false);
        m_oBuyText->setRender(false);
    }
    else 
    {
        bool bUnlocked = ProgressManager::instance().getSavedData().sBabyData[m_iSelectedBaby].bUnlocked;
        
        m_oDetailsIcon->setRender(bUnlocked);
        m_oDetailsText->setRender(bUnlocked);
        m_oBuyIcon->setRender(!bUnlocked);
        m_oBuyText->setRender(!bUnlocked);
    }
}

void StateShopAnimals::showBuyPopup()
{
    //Popup to buy
    BabyData sBabyData = LevelManager::instance().getBabyData(m_iSelectedBaby);
    int iPrice = sBabyData.iPrice;
    if(ProgressManager::instance().getSavedData().iCoins < iPrice)
    {
        m_ePopupState = SHOPPOPUP_NOCOINS;
        
        StatePopUp::instance().popupConfirm("You don't have enough coins to buy this baby! Do you want to get some more coins?");
    }
    else
    {
        char buff[100];
        if(sBabyData.iPrice <=0)
        {
            m_ePopupState = SHOPPOPUP_BUY;
            
            sprintf(buff, "Buy %s in store?", sBabyData.sCaption.c_str());            
        }
        else
        {
            char price[10];
            if(sBabyData.iPrice >= 10000)
                sprintf(price, "%ik", sBabyData.iPrice / 1000);
            else
                sprintf(price, "%i", sBabyData.iPrice);
            
            m_ePopupState = SHOPPOPUP_BUY;
            
            sprintf(buff, "Buy %s for %s coins?", sBabyData.sCaption.c_str(), price);
            
        }
        StatePopUp::instance().popupConfirm(buff);
    }    
}

void StateShopAnimals::updatePaused(float dt)
{
    StateShopCommon::updatePaused(dt);
    
    for(int i=0; i<MAX_BABY_TYPES; ++i)
    {
        if(ProgressManager::instance().getSavedData().sBabyData[i].bUnlocked)
        {
            m_oBabyIcon[i]->update();
        }
    }    
}

void StateShopAnimals::update(float dt)
{
    StateShopCommon::update(dt);

    switch(m_eState)
    {
        case SHOP_IDLE:
            m_oDetailsIcon->update();
            m_oBuyIcon->update();
            
            for(int i=0; i<MAX_BABY_TYPES; ++i)
            {
                m_oBabyShadow[i]->update();
            }
            
            if(m_oDetailsIcon->isClicked() && m_iSelectedBaby != -1)
            {
                bool bUnlocked = ProgressManager::instance().getSavedData().sBabyData[m_iSelectedBaby].bUnlocked;

                if(bUnlocked)
                {
                    StateShopAnimalDetail::instance().setBaby(m_iSelectedBaby);
                    m_oNextState = &StateShopAnimalDetail::instance();
                    m_oExit->start(true);
                    m_eState = SHOP_EXIT;
                }
                else
                {
                    showBuyPopup();
                }
            }
            else 
            {
                for(int i=0; i<MAX_BABY_TYPES; ++i)
                {
                    if(m_oBabyIcon[i]->isClicked())
                    {
                        if(m_iSelectedBaby == i)
                        {
                            if(ProgressManager::instance().getSavedData().sBabyData[i].bUnlocked)
                            {
                                StateShopAnimalDetail::instance().setBaby(m_iSelectedBaby);
                                m_oNextState = &StateShopAnimalDetail::instance();
                                m_oExit->start(true);
                                m_eState = SHOP_EXIT;
                            }
                            else
                            {
                                showBuyPopup();
                            }
                        }
                        
                        m_iSelectedBaby = i;
                        updateButtons();
                        m_oBabySelectedIcon->setPosition(m_oBabyIcon[i]->getPosition());
                        
                        break;
                    }
                }
            }
            break;
            
        default:
            break;
    }
}

void StateShopAnimals::render()
{
    StateShopCommon::render();
    
    if(m_iSelectedBaby != -1)
    {
        m_oBabySelectedIcon->render();
    }

    for(int i=0; i<MAX_BABY_TYPES; i++)
    {
        m_oBabyShadow[i]->render();
        m_oBabyIcon[i]->render();
        //m_oBabyText[i]->render();

        m_oBabyPrice[i]->render();
        m_oBabyCoin[i]->render();
    }

    m_oDetailsIcon->render();
    m_oDetailsText->render();
    m_oBuyIcon->render();
    m_oBuyText->render();
}

void StateShopAnimals::exit()
{
    StateShopCommon::exit();
}

void StateShopAnimals::setBackState()
{
//    m_oNextState = &StateShop::instance();
    m_oNextState = &StateMainMenu::instance();
}
